﻿Public Class FormVendorEditor

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        If txtVendorName.Text.Trim() = "" Then
            MsgBox("Please input vendorName")
            txtVendorName.Focus()
            Exit Sub
        End If

        If txtPhone.Text.Trim() = "" Then
            MsgBox("Please input phone!")
            txtPhone.Focus()
            Exit Sub
        End If

        Cmd.Parameters("@Name") = txtVendorName.Text
        Cmd.Parameters("@Id") = txtVendorId.Text
        Cmd.Parameters("@Phone") = txtPhone.Text
        Dim count As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM Vendor WHERE VendorName = @Name AND Phone=@Phone AND VendorId<>@Id")
        If count > 0 Then
            MsgBox("Vendor name is already exists!")
            txtVendorName.SelectAll()
            txtVendorName.Focus()
            Exit Sub
        End If


        Cmd.Parameters("@VendorName") = txtVendorName.Text
        Cmd.Parameters("@Phone") = txtPhone.Text
        Cmd.Parameters("@Address") = txtAddress.Text
        If Me.txtVendorId.Text = "" Then
            Cmd.ExecuteNonQuery("INSERT INTO Vendor VALUES(@VendorName,@Phone,@Address,1);")
        Else
            Cmd.Parameters("@VendorId") = txtVendorId.Text
            Cmd.ExecuteNonQuery("UPDATE Vendor SET VendorName=@VendorName,Phone=@Phone,Address=@Address WHERE VendorId=@VendorId")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub txtAddress_TextChanged(sender As Object, e As EventArgs) Handles txtAddress.TextChanged

    End Sub

    Private Sub FormVendorEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class